using UnityEngine;
using System.Collections.Generic;

public class cSkinnedBase : MonoBehaviour {
	
	public class cSkinnedInfo {
		
		public Transform []Bones;
		public Mesh sharedMesh;
		public Material sharedMaterial;
		public Matrix4x4 []bindposes;
		public BoneWeight []boneWeights;
		
	}
	
	public List<cSkinnedInfo> _SkinnedInfos = new List<cSkinnedInfo>();
	
	// Use this for initialization
	void Start ()
	{
		GetSkinnedInfo();
	}
	
	public void GetSkinnedInfo()
	{
	
		SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
		
		foreach ( SkinnedMeshRenderer currentRenderer in thisRenderer )
		{
			
			cSkinnedInfo addInfo = new cSkinnedInfo();
			
			var existBones = new System.Collections.Generic.List<Transform>();

			for ( int i = 0 ; i < currentRenderer.bones.Length ; i++ )
			{
				
				if ( currentRenderer.bones[i] )
				{
					
					existBones.Add( currentRenderer.bones[i].transform );
					
				}
			}
			
			addInfo.Bones = existBones.ToArray();
			
			addInfo.sharedMesh = currentRenderer.sharedMesh;
			
			addInfo.sharedMaterial = currentRenderer.sharedMaterial;
			
			addInfo.bindposes = currentRenderer.sharedMesh.bindposes;
			
			addInfo.boneWeights = currentRenderer.sharedMesh.boneWeights;
			
			_SkinnedInfos.Add( addInfo );
		}
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void SetDefaultSkinned()
	{
		
		SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
		
		for ( int i = 0 ; i < thisRenderer.Length && i < _SkinnedInfos.Count ; i++ )
		{
			
			SkinnedMeshRenderer currentRenderer = thisRenderer[i];
			
			cSkinnedInfo orgInfo = _SkinnedInfos[i];

			currentRenderer.bones = orgInfo.Bones;
			
			currentRenderer.sharedMesh = orgInfo.sharedMesh;
			
			currentRenderer.sharedMaterial = orgInfo.sharedMaterial;
			
			currentRenderer.sharedMesh.bindposes = orgInfo.bindposes;
			
			currentRenderer.sharedMesh.boneWeights = orgInfo.boneWeights;
			
		}
		
	}
	
	public void AttachSkinned( SkinnedMeshRenderer []smr )
	{
		SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
		
		for ( int i = 0 ; i < thisRenderer.Length && i < smr.Length ; i++ )
		{
			
			SkinnedMeshRenderer currMR = thisRenderer[i];
			SkinnedMeshRenderer chgMR = smr[i];
			var existBones = new System.Collections.Generic.List<Transform>();
			//var existBonesWeight = new System.Collections.Generic.List<Matrix4x4>();
			
			for (int j = 0; j < chgMR.bones.Length; j++ )
			{
				
				bool bFound = false;
				
				for (int k = 0; k < currMR.bones.Length ; k++)
				{
					if ( chgMR.bones[j].name == currMR.bones[k].name )
					{
								
						existBones.Add(currMR.bones[k].transform);
						//existBonesWeight.Add(chgMR.sharedMesh.bindposes[j]);
						bFound = true;
						break;

					}
					
				}
				
				if ( !bFound )
				{
					
					Debug.LogError("Can't Found Bone : " + chgMR.bones[j].name );
					
				}
				
			}
			
			currMR.sharedMesh = chgMR.sharedMesh;
			currMR.sharedMesh.boneWeights = chgMR.sharedMesh.boneWeights;
			//currMR.sharedMesh.bindposes = existBonesWeight.ToArray();//chgMR.sharedMesh.bindposes;
			currMR.sharedMesh.bindposes = chgMR.sharedMesh.bindposes;
			
			currMR.bones = existBones.ToArray();
			
			currMR.sharedMaterial = chgMR.sharedMaterial;
		}
	}
	
	public void AttachSkinned( cSkinnedBase sbase )
	{
		
		SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
		
		for ( int i = 0 ; i < thisRenderer.Length && i < _SkinnedInfos.Count ; i++ )
		{
			
			SkinnedMeshRenderer currentRenderer = thisRenderer[i];
			
			var existBones = new System.Collections.Generic.List<Transform>();
			
			for ( int j = 0 ; j < _SkinnedInfos[i].Bones.Length ; j++ )
			{
				
				for ( int k = 0 ; k < currentRenderer.bones.Length ; k++ )
				{
					if ( _SkinnedInfos[i].Bones[j].name == currentRenderer.bones[k].name )
					{
						
						existBones.Add( currentRenderer.bones[k].transform );
						
						break;
						
					}
				}
				
			}
/*			
			for ( int j = 0 ; j < currentRenderer.bones.Length ; j++ )
			{
				
				for ( int k = 0 ; k < sbase._SkinnedInfos.Count ; k++ )
				{
					for ( int l = 0 ; l < sbase._SkinnedInfos[k].Bones.Length ; l++ )
					{
						if ( currentRenderer.bones[j].name == sbase._SkinnedInfos[k].Bones[l] )
						{
							
							
							break;
							
						}
					}
				}
			}
						 */
			cSkinnedInfo orgInfo = sbase._SkinnedInfos[i];

			currentRenderer.bones = existBones.ToArray ();//orgInfo.Bones;
			
			currentRenderer.sharedMesh = orgInfo.sharedMesh;
			
			currentRenderer.sharedMaterial = orgInfo.sharedMaterial;
			
			currentRenderer.sharedMesh.bindposes = orgInfo.bindposes;
			
			currentRenderer.sharedMesh.boneWeights = orgInfo.boneWeights;
			
		}

//		sbase._SkinnedInfos;
		
	}
	
}
